<template>
  <van-popup
    v-model="value"
    position="bottom"
    :close-on-click-overlay="false"
    @click-overlay="onClose"
  >
    <div>
      <div class="van-hairline--top-bottom van-actionsheet__header">
        <div>{{title}}</div>
        <van-icon name="close" @click="onClose"/>
      </div>
      <van-cell-group class="list">
        <van-cell class="item" v-for="(item,index) in list" :key="index">
          <van-row type="flex">
            <van-col span="24">
              <div class="title">{{item.mansong_name}}</div>
              <div class="name">{{item.showInfo}}</div>
              <div class="time">{{item.showTerm}}</div>
            </van-col>
          </van-row>
        </van-cell>
      </van-cell-group>
    </div>
    <div class="foot-btn-group">
      <van-button size="normal" block round type="default" @click="onClose">关闭</van-button>
    </div>
  </van-popup>
</template>

<script>
import { formatDate } from "@/utils/util";
export default {
  data() {
    return {};
  },
  props: {
    title: String,
    value: {
      type: Boolean,
      default: false
    },
    items: {
      type: Array
    }
  },
  computed: {
    list() {
      const $this = this;
      const newItems = $this.items.map(e => {
        let rules = e.rules.map((r, i) => {
          let discount = parseFloat(r.discount);
          let price = parseFloat(r.price);
          let free_shipping = parseInt(r.free_shipping);
          let text = "";
          if (discount > 0) {
            text += `${i += 1}、满${price}减${discount}元； `;
          }
          if (free_shipping) {
            text += `${i += 1}、满${price}包邮； `;
          }
          if (r.coupon_type_id) {
            text += `${i += 1}、满${price}送优惠券(${r.coupon_type_name})； `;
          }
          if (r.gift_card_id) {
            text += `${i += 1}、满${price}送礼品券(${r.gift_voucher_name})； `;
          }
          if (r.gift_id) {
            text += `${i += 1}、满${price}送赠品(${r.gift_name})； `;
          }
          return text;
        });
        let str = "";
        rules.forEach(r => {
          str += r;
        });
        e.showInfo = str;
        e.showTerm =
          "使用期限 " +
          formatDate(e.start_time) +
          " ~ " +
          formatDate(e.end_time);
        return e;
      });

      return newItems;
    }
  },
  methods: {
    onClose() {
      this.$emit("input", false);
    }
  }
};
</script>
<style scoped>
.list {
  max-height: 60vh;
  overflow-y: auto;
}

.item .van-col > div {
  line-height: 1.4;
}

.item .title {
  font-weight: 700;
  color: #ff454e;
}

.item .name {
  margin-bottom: 4px;
}

.item .time {
  font-size: 12px;
  color: #606266;
}
</style>
